using System.Collections.Generic;
using System.Linq;
using Moq;
using NUnit.Framework;
using WB.Mvc.Forms;

namespace WB.Forms.Tests
{
	[TestFixture]
	public class ModelFieldGroupStrategyTestFixture
	{
		#region Setup/Teardown

		[SetUp]
		public void InitializeTest()
		{
			strategy = new ModelFieldGroupStrategy();
		}

		#endregion

		[Test]
		public void ShouldBeModelFieldGroup()
		{
			var groups = strategy.Create(new Parent());

			foreach (FieldGroup grp in groups.Select(x => x.Key)) 
				Assert.IsInstanceOfType(typeof (ModelFieldGroup), grp);
		}

		[Test]
		public void ShouldCreateFieldGroups()
		{
			var groups = strategy.Create(new Parent());

			Assert.IsNotNull(groups, "Should not be null.");
			Assert.IsNotEmpty(groups.ToList(), "Should not be empty.");
		}

		[Test]
		public void ShouldCreateOneFieldGroup()
		{
			var groups = strategy.Create(new Child());

			Assert.AreEqual(1, groups.Count());
		}

		[Test]
		public void ShouldCreateTwoFieldGroups()
		{
			var groups = strategy.Create(new Parent());

			Assert.AreEqual(2, groups.Count());
		}

		[Test]
		public void ShouldUseUserStrategy()
		{
			var strategyMock = new Mock<IFieldStrategy>();
			strategyMock.Expect(x => x.Create(It.IsAny<string>())).Returns(new CharField("char-field")).Verifiable();

			var userStrategies = new List<IFieldStrategy> {strategyMock.Object};

			strategy = new ModelFieldGroupStrategy(userStrategies.ToArray());
			strategy.Create(new Parent());

			strategyMock.Verify();
		}

		private ModelFieldGroupStrategy strategy;
	}
}